package com.ehl.capture.db;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisUtil
{
    public static Logger logger = Logger.getLogger(RedisUtil.class);
    private static String redisHost;
    private static int redisPort;
    private static String redisPassword;
    private static long period;
    private static int maxTotal = 300;
    private static int maxIdle = 100;
    private static int minItal = 10;
    private static long maxWaitMillis = 5000L;

    private static volatile JedisPool jedisPool = null;

    static {
        redisHost = Config.getValue("zdkk.redischeck.host");
        String port = Config.getValue("zdkk.redischeck.port");
        String pssword = Config.getValue("zdkk.redischeck.password");
        String periodSet = Config.getValue("zdkk.redischeck.period");
        maxTotal = Integer.parseInt(Config.getValue("zdkk.redischeck.maxTotal"));
        maxIdle = Integer.parseInt(Config.getValue("zdkk.redischeck.maxIdle"));
        minItal = Integer.parseInt(Config.getValue("zdkk.redischeck.minItal"));
        maxWaitMillis = Integer.parseInt(Config.getValue("zdkk.redischeck.maxWaitMillis"));

        if (StringUtils.isNotBlank(port)) {
            redisPort = Integer.valueOf(port).intValue();
        }
        if (StringUtils.isBlank(pssword)) {
            redisPassword = null;
        }
        if (StringUtils.isNotBlank(periodSet)) {
            period = Long.valueOf(periodSet).longValue();
        }
        logger.info("监测重点卡口Redis host= " + redisHost);
        logger.info("监测重点卡口Redis port= " + redisPort);
        logger.info("监测重点卡口Redis password= " + redisPassword);
        logger.info("监测重点卡口间隔时长= " + period);
        logger.info("监测重点卡口Redis maxTotal=" + maxTotal);
        logger.info("监测重点卡口Redis maxIdle=" + maxIdle);
        logger.info("监测重点卡口Redis minItal=" + minItal);
        logger.info("监测重点卡口Redis maxWaitMillis=" + maxWaitMillis);
    }

    public static JedisPool getJedisPoolInstance(String host, int port, String password){
        if (null == jedisPool) {
            synchronized (RedisUtil.class) {
                if (null == jedisPool) {
                    JedisPoolConfig poolConfig = new JedisPoolConfig();
                    poolConfig.setMaxTotal(maxTotal);
                    poolConfig.setMaxIdle(maxIdle);
                    poolConfig.setMinIdle(minItal);
                    poolConfig.setMaxWaitMillis(maxWaitMillis);
                    poolConfig.setTestOnBorrow(true);
                    poolConfig.setTestOnReturn(true);
                    poolConfig.setTimeBetweenEvictionRunsMillis(10000L);
                    poolConfig.setMinEvictableIdleTimeMillis(30000L);
                    poolConfig.setNumTestsPerEvictionRun(-1);
                    jedisPool = new JedisPool(poolConfig, host, port, 1000, password);
                }
            }
        }
        return jedisPool;
    }

    public static long getPeriod() {
        return period;
    }

    public static Jedis getJedis() {
        JedisPool jedisPool = getJedisPoolInstance(redisHost, redisPort, redisPassword);
        return jedisPool.getResource();
    }

    public static Jedis getJedis(String host, int port, String password) {
        JedisPool jedisPool = getJedisPoolInstance(host, port, password);
        return jedisPool.getResource();
    }

    public static void closeJedis(Jedis jedis) {
        if (jedis != null)
            jedis.close();
    }

}